﻿using System;
using System.Collections.Generic;
//using System.Data.Objects;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Castle.Windsor;
using laba2.Context;
using laba2.Entity;
using laba2.IoC;
using laba2.Repository.IRepository;
using laba2.Repository.RepositoryImpl;

namespace laba2.Services
{
    public class CourseService
    {
/*        private ICourseRepository _courseRepository;


        public CourseService(ICourseRepository courseRepository)
        {
            
        }*/

        public CourseService()
        {
        }

        public  List<Course> GetAll()
        {
            using (var context=new Lab3DBContext())
            {
               return  context.Courses.ToList();
            }
        } 

        public void AddCourse(string name)
        {
            using (var context=new Lab3DBContext())
            {
                context.Courses.Add(new Course(name));
                context.SaveChanges();
            }
        }

        public Course GetByName(string name)
        {
            using (var context=new Lab3DBContext())
            {
                var q = from course in context.Courses
                        where course.Name == name
                        select course;
                //Console.WriteLine(((ObjectQuery<IQueryable>) q ).ToTraceString());
                return q.ToList()[0];
            }
        }

        public Course GetById(int id)
        {
            using (var context=new Lab3DBContext())
            {
                return context.Courses.Find(id);
            }
        }

        public void SetEvaluation(int courseId, int minimum, int maximum, string evaluation)
        {
            using (var context=new Lab3DBContext())
            {
                context.Evaluations.Add(new Evaluation(evaluation, maximum, minimum,courseId));
                context.SaveChanges();
            }
        }

        public string GetEvaluation(int idCourse, int rating)
        {
            using (var context=new Lab3DBContext())
            {
                var evaluations = context.Evaluations.Where(e => e.CourseId == idCourse).ToList();
                foreach (var evaluation in evaluations)
                {
                    if (evaluation.isEvaluation(rating)) return evaluation.Evaluation1;
                }
                return null;
            }
        }
         
       /* public CourseRepository CourseRepository
        {
            get { return _courseRepository; }
            set { _courseRepository = value; }
        }*/
    }
}
